/*
 *  mathOperations.c
 *  graphics1
 *
 *  Created by Javier Quevedo on 12/9/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "mathOperations.h"
#include <math.h>
#include <stdio.h>
#if defined(WIN32)
#include <malloc.h>
#endif
#define Pi 3.14159
#define elipsoidCircleRadious 4




float elipsoidRotationforAngle(float _theta){
	coord2d *objectCoords = malloc(sizeof(coord2d));
	double theta;
	double x;
	double y;
	
	elipsoidCoordsforAngle(_theta, objectCoords);
	
if ( (243.4349 < _theta) && (_theta < 296.5651))
	{
		return 180;
	}
	
	if ( (116.5651  >= _theta) && ( _theta >= 63.4349))
	{
		return 0;	
		
		
	}
	
	if ( (243.4349 >= _theta) && (_theta >= 116.5651))
	{
		
		x = objectCoords->x - (-2);
		y = objectCoords->y;
		
		theta = atan(y / x);
		theta = (theta * 360.0 / (2.0 * Pi));
		theta = 270 - theta;
		
		
		
		
		return (float)theta;		
		
	}
	//  ( 296.573 <= theta <= 63.426)
	
	x = objectCoords->x - (2.0);

	y = objectCoords->y;
	
	theta = atan(y / x);
	theta = (theta * 360.0 / (2.0 * Pi));
	//theta = theta;
	return  (float)(90.0 - theta);	
	
	
}
void elipsoidCoordsforAngle(float _theta, coord2d *coords){

	double r;
	double theta = _theta * 2 * Pi / 360.0;
	if ( (243.4349 < _theta) && (_theta < 296.5651))
	{
		r = -elipsoidCircleRadious / sin(theta);
		coords->x = r * cos (theta);
		coords->y = r* sin(theta);
		return;	
		
	}
	
	if ( (116.5651  >= _theta) && ( _theta >= 63.4349))
	{
		r = elipsoidCircleRadious / sin(theta);
		coords->x = r * cos (theta);
		coords->y = r* sin(theta);
		return;		
	
		
	}
	
	if ( (243.4349 >= _theta) && (_theta >= 116.5651))
	{
	
		r = ((-elipsoidCircleRadious * cos(theta)) + (elipsoidCircleRadious * sqrt(3+pow(cos(theta),2))))/2; 
		
		coords->x = r * cos (theta);
		coords->y = r* sin(theta);
		return;		
		
	}
	//  ( 296.573 <= theta <= 63.426)
	r = ((elipsoidCircleRadious * cos(theta)) + (elipsoidCircleRadious * sqrt(3+pow(cos(theta),2))))/2; 

	coords->x = r * cos (theta);
	coords->y = r* sin(theta);
	

	return;		
}
